﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using DotNetNuke.Services.Localization;

namespace iLangl.Customer.DNN.Entities
{
	public class ContentPresenter
	{
		private dtoContent _content;
		public ContentPresenter(dtoContent content)
		{
			_content = content;
		}

		public dtoContent Content
		{
			get
			{
				return _content;
			}
		}

		public String CultureCode { get; set; }

		private Hashtable Contents = new Hashtable();
		public void AddContent(object key, object content)
		{
			Contents[key] = content;
		}

		public void AddContent(dtoContent.ContentRow cr, object content)
		{
			AddContent(GetContentKey(cr), content);
		}

		private Hashtable Nodes = new Hashtable();
		public void AddTreeNode(object key, object node)
		{
			Nodes[key] = node;
		}

		public void AddTreeNode(dtoContent.ContentRow cr, object node)
		{
			AddTreeNode(GetContentKey(cr), node);
		}

		public object GetContent(object key)
		{
			return Contents[key];
		}

		public object GetContent(dtoContent.ContentRow cr)
		{
			return Contents[GetContentKey(cr)];
		}

		public String GetContentKey(dtoContent.ContentRow cr)
		{
			return string.Format("{0}_{1}_{2}", cr.ContentType, cr.ContentId, cr.CultureCode);
		}

		public object GetNode(object key)
		{
			return Nodes[key];
		}

		public object GetNode(dtoContent.ContentRow cr)
		{
			return Nodes[GetContentKey(cr)];
		}

		internal void AddLanguagePair(string sourceLanguage, string targetLanguage)
		{
			dtoContent.LanguagesRow source =  Content.Languages.FindByLanguageId(sourceLanguage);

			if (source == null)
			{
				Locale locale = LanguageManager.Instance.GetLanguage(sourceLanguage);

				source = Content.Languages.AddLanguagesRow(locale.Code, locale.Text);
			}

			dtoContent.LanguagesRow target = Content.Languages.FindByLanguageId(targetLanguage);
			if (target == null)
			{
				Locale locale = LanguageManager.Instance.GetLanguage(targetLanguage);

				target = Content.Languages.AddLanguagesRow(locale.Code, locale.Text);
			}

			Content.LanguagePairs.AddLanguagePairsRow(source, target);
		}
	}
}